/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.wallet.presenter;

import com.suozhang.framework.utils.T;
import com.yiqi.liebang.entity.bo.BanlanceBo;
import com.yiqi.liebang.entity.bo.PageBo;
import com.yiqi.liebang.entity.bo.PayBo;
import com.yiqi.liebang.entity.bo.PublicBo;
import com.yiqi.liebang.entity.bo.RechargeBo;
import com.yiqi.liebang.feature.wallet.contract.WalletContract;
import com.yiqi.liebang.feature.wallet.dependencies.wallet.DaggerWalletPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class WalletPresenter implements WalletContract.Presenter {

    private WalletContract.View mView;

    @Inject

    WalletContract.Model mModel;


    public WalletPresenter(WalletContract.View mView) {
        this.mView = mView;
        DaggerWalletPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void userRechagre(int payStatus, double transactionamount) {
        if (transactionamount <= 0) {
            T.showShort("请输入有金额!");
            return;
        }
        mModel.userRechagre(new RechargeBo(payStatus, transactionamount)).compose(mView.bindToLife()).subscribe(new Observer<PayBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(PayBo s) {
                mView.dismissLoading();
                mView.showPaySuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void userWithdraw(int payStatus, double transactionamount, String payeeAccount,String userOpenid) {
        mModel.userWithdraw(new RechargeBo(payStatus, transactionamount, payeeAccount,userOpenid,"")).compose(mView.bindToLife()).subscribe(new Observer<PublicBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(PublicBo s) {
                mView.dismissLoading();
                mView.userWithdrawSucc(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getUserAssetsDetail(int pageIndex,
                                    int pageSize) {
        mModel.getUserAssetsDetail(new PageBo(pageIndex, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<BanlanceBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(BanlanceBo banlanceBos) {
                if (banlanceBos==null) return;
                 mView.showSuccess(banlanceBos.getBalance()+"");
                if (banlanceBos.getWallet() != null && !banlanceBos.getWallet().isEmpty()) {
                    mView.showTransactionListSuccess(banlanceBos.getWallet());
                } else {
                    mView.showTransactionListEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void getUserAssetsDetailMore(int pageIndex,
                                        int pageSize) {
        mModel.getUserAssetsDetail(new PageBo(pageIndex, pageSize))
                .compose(mView.bindToLife()).subscribe(new Observer<BanlanceBo>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(BanlanceBo banlanceBo) {
                mView.dismissLoading();
                mView.showTransactionListMoreSuccess(banlanceBo.getWallet());

            }

            @Override
            public void onError(Throwable e) {
                mView.showTransactionListMoreError(e.getMessage());
                mView.dismissLoading();
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }
}
